package com.example.yls.demoa;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.ShareActionProvider;

public class MainActivity extends AppCompatActivity {
private Button btnSave;
private Button btnRead;
private EditText edtName;
private EditText edtAge;
private RadioButton radioMan;
private RadioButton radioGirl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnRead=findViewById(R.id.btn_save);
        btnSave=findViewById(R.id.btn_read);
        edtAge=findViewById(R.id.edt_age);
        edtName=findViewById(R.id.edt_name);
        radioGirl=findViewById(R.id.radio_girl);
        radioMan=findViewById(R.id.radio_man);



        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences sp=getSharedPreferences("mysp", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor=sp.edit();
                editor.putBoolean("isBoy",true);
                editor.putString("name","lisi");
                editor.putInt("age",18);
                editor.commit();
                Log.e("MainActivity","commit");
            }
        });
    btnRead.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        SharedPreferences sp=getSharedPreferences("mysp", Context.MODE_PRIVATE);
        boolean isBoy=sp.getBoolean("isBoy",false);
        String name=sp.getString("name",null);
        int age=sp.getInt("age",0);
            Log.e("MainActivity","read isBoy = "+ isBoy +" name "+name +" age "+age);
        }
    });
    }

    @Override
    protected void onStop() {
        String name=edtName.getText().toString();
        int age=Integer.parseInt(edtAge.getText().toString());
        boolean isBoy=radioMan.isChecked();
        saveTosp(name,age,isBoy);
        super.onStop();
    }

    private void saveTosp(String name, int age, boolean isBoy) {
        SharedPreferences sp=getSharedPreferences("person", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putBoolean("isBoy",isBoy);
        editor.putString("name",name);
        editor.putInt("age",age);
        editor.commit();
    }

    @Override
    protected void onResume() {
        super.onResume();
        SharedPreferences sp=getSharedPreferences("person", Context.MODE_PRIVATE);
        boolean isBoy=sp.getBoolean("isBoy",false);
        String name=sp.getString("name",null);
        int age=sp.getInt("age",0);
        edtName.setText(name);
        edtAge.setText(String.valueOf(age));
        radioMan.setChecked(isBoy);
        radioGirl.setChecked(!isBoy);
    }
}
